package com.example.controller;


import com.example.pojo.Poem;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author luqi
 * @Date 2025 10 21 09 10
 **/
@RestController
@RequestMapping("/poems")
@CrossOrigin
public class PoemController {
    private List<Poem>  poemList = new ArrayList<>();

    {
        poemList.add(new Poem(1, "司马迁", "报任安书","人固有一死，或重于泰山，或轻于鸿毛，用之所趋异也。"));
        poemList.add(new Poem(2, "管仲", "管子","仓廪实而知礼节，衣食足而知荣辱。"));
        poemList.add(new Poem(3,"李白","静夜思","举头望明月，低头思故乡"));
        poemList.add(new Poem(4,"李白","静夜思","举头望明月，低头思故乡"));
        poemList.add(new Poem(5,"李白","静夜思","举头望明月，低头思故乡"));
        poemList.add(new Poem(6,"李白","静夜思","举头望明月，低头思故乡"));
    }

    @PostMapping("/add")
    public String add(@RequestBody Poem poem){
        poemList.add(poem);
        return "新增成功";
    }

    @GetMapping("/show")
    public List<Poem> getAll(){
        return poemList;
    }

    @GetMapping("/search")
    public List<Poem> search(String author, String source){
        return poemList.stream().filter(a -> a.getAuthor().equals(author) || a.getSource().equals(source)).collect(Collectors.toList());
    }

    @DeleteMapping("delete/{id}")
    public String delete(@PathVariable Integer id){
        boolean flag = poemList.removeIf(poem -> poem.getId().equals(id));
        return flag ? "删除成功" : "删除失败";
    }
}
